/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.eam.clinica.vista.gui.varios;

import co.edu.eam.clinica.vista.controladores.varios.controladorFuncionario;
import co.edu.eam.clinica.vista.gui.common.PanelOncologos;
import co.edu.eam.persitencia.entidades.Especialidad;
import co.edu.eam.persitencia.entidades.Funcionario;
import co.edu.eam.persitencia.entidades.Medico;
import co.edu.eam.persitencia.enumeraciones.TipoDocumentoEnum;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author USUARIO
 */
public class PanelFuncionario extends PanelOncologos {

    public controladorFuncionario conf;
    

    public PanelFuncionario() {
        initComponents();
        llenarCEspecialidades();        
        llenarCTipoDoc();
        conf = new controladorFuncionario();
        setVisible(true);


        




    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel8 = new javax.swing.JLabel();
        tfDocumento = new javax.swing.JTextField();
        cbTipoDoc = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        tfNombre = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        tfApellido = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jCbCargo = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jTxLogin = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        paneldatosMedico = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jTxNit = new javax.swing.JTextField();
        jTxRegMedico = new javax.swing.JTextField();
        jCbEspecialida = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jtpass = new javax.swing.JPasswordField();
        bBuscar = new javax.swing.JButton();
        bEditar = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        jLabel8.setText("Documento");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Primer Nombre");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Primer Apellido");

        jLabel3.setText("Cargo:");

        jCbCargo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "Funcionario", "Medico" }));
        jCbCargo.setToolTipText("");
        jCbCargo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCbCargoItemStateChanged(evt);
            }
        });
        jCbCargo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCbCargoActionPerformed(evt);
            }
        });

        jLabel4.setText("Login:");

        jLabel5.setText("Password:");

        paneldatosMedico.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Medicos"));

        jLabel6.setText("Nit:");

        jLabel7.setText("Registro Medico:");

        jLabel9.setText("Especialidad:");

        jCbEspecialida.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCbEspecialidaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout paneldatosMedicoLayout = new javax.swing.GroupLayout(paneldatosMedico);
        paneldatosMedico.setLayout(paneldatosMedicoLayout);
        paneldatosMedicoLayout.setHorizontalGroup(
            paneldatosMedicoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paneldatosMedicoLayout.createSequentialGroup()
                .addGroup(paneldatosMedicoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel9))
                .addGap(44, 44, 44)
                .addGroup(paneldatosMedicoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTxRegMedico, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
                    .addComponent(jCbEspecialida, 0, 310, Short.MAX_VALUE)
                    .addComponent(jTxNit, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE))
                .addContainerGap())
        );
        paneldatosMedicoLayout.setVerticalGroup(
            paneldatosMedicoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paneldatosMedicoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(paneldatosMedicoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTxNit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(paneldatosMedicoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTxRegMedico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(paneldatosMedicoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jCbEspecialida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        jButton1.setText("Guardar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jtpass.setText("jPasswordField1");

        bBuscar.setText("Buscar");
        bBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBuscarActionPerformed(evt);
            }
        });

        bEditar.setText("Editar");
        bEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bEditarActionPerformed(evt);
            }
        });

        jButton2.setText("Limpiar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel8)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(17, 17, 17)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(tfDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(34, 34, 34)
                                .addComponent(cbTipoDoc, 0, 166, Short.MAX_VALUE))
                            .addComponent(tfNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
                            .addComponent(tfApellido, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jtpass, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jCbCargo, javax.swing.GroupLayout.Alignment.LEADING, 0, 213, Short.MAX_VALUE)
                                    .addComponent(jTxLogin, javax.swing.GroupLayout.Alignment.LEADING))
                                .addGap(0, 140, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bBuscar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(bEditar)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(paneldatosMedico, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(tfDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbTipoDoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(tfApellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(2, 2, 2)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jCbCargo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTxLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jtpass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(paneldatosMedico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(bBuscar)
                    .addComponent(bEditar)
                    .addComponent(jButton2))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
        if (jCbCargo.getSelectedIndex() == 1) {
            try {
                Funcionario fun = new Funcionario();
                fun.setDocumento(tfDocumento.getText());
                fun.setTipoDocumento((TipoDocumentoEnum) cbTipoDoc.getSelectedItem());
                fun.setPrimerNombre(tfNombre.getText());
                fun.setPrimerApellido(tfApellido.getText());
                fun.setLogin(jTxLogin.getText());
                fun.setPassword(jtpass.getText());
                conf.crearFuncionario(fun);
                JOptionPane.showMessageDialog(null, "El Funciionario fue Creado");
            } catch (Exception ex) {
                Logger.getLogger(PanelFuncionario.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            if (jCbCargo.getSelectedIndex() == 2) {
                try {
                    Medico me = new Medico();

                    me.setDocumento(tfDocumento.getText());
                    me.setTipoDocumento((TipoDocumentoEnum) cbTipoDoc.getSelectedItem());
                    me.setPrimerNombre(tfNombre.getText());
                    me.setPrimerApellido(tfApellido.getText());
                    me.setLogin(jTxLogin.getText());
                    me.setPassword(jtpass.getText());
                    me.setNIT(jTxNit.getText());
                    me.setRegistroMedico(jTxRegMedico.getText());
                    me.setEspecialidad((Especialidad) jCbEspecialida.getSelectedItem());
                    conf.crearMedico(me);
                    JOptionPane.showMessageDialog(null, "El Medico fue Creado");
                } catch (Exception ex) {
                    Logger.getLogger(PanelFuncionario.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }


    }//GEN-LAST:event_jButton1ActionPerformed

    private void bBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBuscarActionPerformed
        try {
            
            String documento=tfDocumento.getText();
            Funcionario f=conf.buscarFuncionario(documento);
            Medico m=conf.buscarMedico(documento);
            if (f!=null&&m!=null) {
                cbTipoDoc.setSelectedItem(f.getTipoDocumento());
                tfNombre.setText(f.getPrimerNombre());
                tfApellido.setText(f.getPrimerApellido());
                jTxLogin.setText(f.getLogin());
                jtpass.setText(f.getPassword());
                jTxNit.setText(m.getNIT());
                jTxRegMedico.setText(m.getRegistroMedico());
                jCbEspecialida.setSelectedItem(m.getEspecialidad());
                
            }
        } catch (Exception ex) {
            Logger.getLogger(PanelFuncionario.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_bBuscarActionPerformed

    private void bEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bEditarActionPerformed
        try {
            Funcionario f=new Funcionario();
            Medico m=new Medico();
            cbTipoDoc.setSelectedItem(f.getTipoDocumento());
                    tfNombre.setText(f.getPrimerNombre());
                    tfApellido.setText(f.getPrimerApellido());
                    jTxLogin.setText(f.getLogin());
                    jtpass.setText(f.getPassword());
                    jTxNit.setText(m.getNIT());
                    jTxRegMedico.setText(m.getRegistroMedico());
                    jCbEspecialida.setSelectedItem(m.getEspecialidad());
                    conf.editarFuncionario(f);
                    conf.editarMedico(m);
        } catch (Exception ex) {
            Logger.getLogger(PanelFuncionario.class.getName()).log(Level.SEVERE, null, ex);
        }


    }//GEN-LAST:event_bEditarActionPerformed

    private void jCbCargoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCbCargoItemStateChanged

        if (jCbCargo.getSelectedIndex() == 0) {
            tfDocumento.enable(false);
            cbTipoDoc.enable(false);
            tfNombre.enable(false);
            tfApellido.enable(false);
            jTxLogin.enable(false);
            jtpass.enable(false);
            jTxNit.enable(false);
            jTxRegMedico.enable(false);
            jCbEspecialida.enable(false);
         }else{
            if (jCbCargo.getSelectedIndex()==1) {
                tfDocumento.enable(true);
            cbTipoDoc.enable(true);
            tfNombre.enable(true);
            tfApellido.enable(true);
            jTxLogin.enable(true);
            jtpass.enable(true);
            jTxNit.enable(false);
            jTxRegMedico.enable(false);
            jCbEspecialida.enable(false);
                
            }else{
                if (jCbCargo.getSelectedIndex()==2) {
                     tfDocumento.enable(true);
            cbTipoDoc.enable(true);
            tfNombre.enable(true);
            tfApellido.enable(true);
            jTxLogin.enable(true);
            jtpass.enable(true);
            jTxNit.enable(true);
            jTxRegMedico.enable(true);
            jCbEspecialida.enable(true);
                    
                }
            }
        }
    }//GEN-LAST:event_jCbCargoItemStateChanged

    private void jCbCargoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCbCargoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCbCargoActionPerformed

    private void jCbEspecialidaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCbEspecialidaActionPerformed
        // TODO add your handling code here:
        
        
    }//GEN-LAST:event_jCbEspecialidaActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
            jCbCargo.setSelectedIndex(0);
            tfDocumento.setText("");
            cbTipoDoc.setSelectedIndex(0);
            tfNombre.setText("");
            tfApellido.setText("");
            jTxLogin.setText("");
            jtpass.setText("");
            jTxNit.setText("");
            jTxRegMedico.setText("");
            jCbEspecialida.setSelectedIndex(0);
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bBuscar;
    private javax.swing.JButton bEditar;
    private javax.swing.JComboBox cbTipoDoc;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jCbCargo;
    private javax.swing.JComboBox jCbEspecialida;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField jTxLogin;
    private javax.swing.JTextField jTxNit;
    private javax.swing.JTextField jTxRegMedico;
    private javax.swing.JPasswordField jtpass;
    private javax.swing.JPanel paneldatosMedico;
    private javax.swing.JTextField tfApellido;
    private javax.swing.JTextField tfDocumento;
    private javax.swing.JTextField tfNombre;
    // End of variables declaration//GEN-END:variables


private void llenarCEspecialidades() {
        try {
            List<Especialidad> lista = conf.listarEspecialidades();
            for (int i = 0; i < lista.size(); i++) {
                Especialidad esp = lista.get(i);
                jCbEspecialida.addItem(esp);
                
            }
        } catch (Exception ex) {
            Logger.getLogger(PanelFuncionario.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

private void llenarCTipoDoc() {
        try {
            TipoDocumentoEnum[] tipos = TipoDocumentoEnum.values();            
            for (int i = 0; i < tipos.length; i++) {
                TipoDocumentoEnum esp = tipos[i];
                cbTipoDoc.addItem(esp);
                
            }
        } catch (Exception ex) {
            Logger.getLogger(PanelFuncionario.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
}